#!/bin/bash

# Metrics SDK 一键部署脚本
# Author: yelink
# Date: 2025-08-13

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的信息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查Maven是否安装
check_maven() {
    print_info "检查Maven是否安装..."
    if ! command -v mvn &> /dev/null; then
        print_error "Maven未安装，请先安装Maven"
        exit 1
    fi
    print_success "Maven已安装: $(mvn -version | head -n 1)"
}

# 检查Java版本
check_java() {
    print_info "检查Java版本..."
    if ! command -v java &> /dev/null; then
        print_error "Java未安装，请先安装JDK 21"
        exit 1
    fi
    
    java_version=$(java -version 2>&1 | head -n 1 | cut -d '"' -f 2)
    major_version=$(echo "$java_version" | cut -d '.' -f 1)
    
    if [ "$major_version" -lt 21 ]; then
        print_error "当前Java版本: $java_version，需要JDK 21或更高版本"
        exit 1
    fi
    
    print_success "Java版本符合要求: $java_version"
}

# 清理并编译
build_project() {
    print_info "开始清理和编译项目..."
    mvn clean compile
    
    if [ $? -eq 0 ]; then
        print_success "项目编译成功"
    else
        print_error "项目编译失败"
        exit 1
    fi
}

# 运行测试
run_tests() {
    print_info "运行测试..."
    mvn test
    
    if [ $? -eq 0 ]; then
        print_success "测试通过"
    else
        print_warning "测试失败，但继续执行部署"
    fi
}

# 打包项目
package_project() {
    print_info "打包项目..."
    mvn package -DskipTests
    
    if [ $? -eq 0 ]; then
        print_success "项目打包成功"
    else
        print_error "项目打包失败"
        exit 1
    fi
}

# 部署到私服
deploy_to_repository() {
    print_info "部署到私服 maven.yelinksaas.com..."
    
    # 检查settings.xml中是否配置了服务器认证信息
    print_warning "请确保在 ~/.m2/settings.xml 中配置了 yelink-maven-releases 和 yelink-maven-snapshots 的认证信息"
    
    mvn deploy -DskipTests
    
    if [ $? -eq 0 ]; then
        print_success "部署成功！"
        print_info "您可以在以下地址查看部署的构件："
        echo "  Release仓库: https://maven.yelinksaas.com/repository/maven-releases/"
        echo "  Snapshot仓库: https://maven.yelinksaas.com/repository/maven-snapshots/"
    else
        print_error "部署失败"
        print_warning "请检查网络连接和认证信息"
        exit 1
    fi
}

# 显示项目信息
show_project_info() {
    print_info "项目信息："
    echo "  项目名称: metrics-sdk"
    echo "  当前版本: 5.0"
    echo "  Spring Boot: 3.3.2"
    echo "  Java版本: 21"
    echo "  构件ID: com.yelink:metrics-sdk:5.0"
}

# 检查settings.xml配置
check_maven_settings() {
    print_info "检查Maven settings.xml配置..."
    
    settings_file="$HOME/.m2/settings.xml"
    if [ ! -f "$settings_file" ]; then
        print_warning "未找到 $settings_file"
        print_info "创建示例配置文件..."
        
        mkdir -p "$HOME/.m2"
        cat > "$settings_file" << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
          http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
    <servers>
        <server>
            <id>yelink-maven-releases</id>
            <username>YOUR_USERNAME</username>
            <password>YOUR_PASSWORD</password>
        </server>
        <server>
            <id>yelink-maven-snapshots</id>
            <username>YOUR_USERNAME</username>
            <password>YOUR_PASSWORD</password>
        </server>
    </servers>
</settings>
EOF
        print_warning "已创建示例 settings.xml，请修改其中的用户名和密码"
        print_error "请先配置正确的认证信息后再运行此脚本"
        exit 1
    else
        print_success "找到 settings.xml 配置文件"
    fi
}

# 主函数
main() {
    echo "=========================================="
    echo "      Metrics SDK 一键部署脚本"
    echo "=========================================="
    
    show_project_info
    echo ""
    
    # 执行检查和构建步骤
    check_maven
    check_java
    check_maven_settings
    
    echo ""
    print_info "开始构建和部署流程..."
    
    build_project
    run_tests
    package_project
    deploy_to_repository
    
    echo ""
    print_success "=========================================="
    print_success "         部署完成！"
    print_success "=========================================="
}

# 脚本帮助信息
show_help() {
    echo "Metrics SDK 一键部署脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  -h, --help     显示此帮助信息"
    echo "  -s, --skip-tests   跳过测试"
    echo "  -c, --clean-only   仅清理和编译，不部署"
    echo ""
    echo "示例:"
    echo "  $0              # 完整的构建和部署"
    echo "  $0 -s           # 跳过测试的构建和部署"
    echo "  $0 -c           # 仅清理和编译"
}

# 解析命令行参数
case "$1" in
    -h|--help)
        show_help
        exit 0
        ;;
    -s|--skip-tests)
        SKIP_TESTS=true
        ;;
    -c|--clean-only)
        CLEAN_ONLY=true
        ;;
esac

# 根据参数执行不同的操作
if [ "$CLEAN_ONLY" = true ]; then
    check_maven
    check_java
    build_project
    print_success "清理和编译完成"
elif [ "$SKIP_TESTS" = true ]; then
    # 修改run_tests函数，跳过测试
    run_tests() {
        print_info "跳过测试..."
    }
    main
else
    main
fi
